﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.View;

namespace ServiceCore.Service
{
    /// <summary>
    /// 数据服务
    /// </summary>
    public partial class RecordService : SqlsugarServices<RecordInfo>
    {
        /// <summary>
        /// 分页 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<RecordInfo, List<V_Record>> Record_List(BaseParam<RecordInfo, List<V_Record>> arg)
        {
            arg.Data = this.Context.Queryable<V_Record>()
                .WhereIF(arg.Param != null && arg.Param.SiteId > 0, x => x.SiteId == arg.Param.SiteId)
                .OrderBy(x => x.CreateTime)
                .ToList();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<RecordInfo, V_Record> Record_Exists(BaseParam<RecordInfo, V_Record> arg, bool? isAdd = null)
        {
            arg.Data = this.Context.Queryable<V_Record>()
                .WhereIF(arg.Id > 0, x => x.Id == arg.Id)
                .First();

            return arg;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<RecordInfo, long> Record_Add(BaseParam<RecordInfo, long> arg)
        {
            arg = Record_Add_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = InsertReturnBigIdentity(arg.Param);
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<List<RecordInfo>, long> Record_AddList(BaseParam<List<RecordInfo>, long> arg)
        {
            arg = Record_AddList_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            this.Context.Insertable<RecordInfo>(arg.Param).ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<RecordInfo, long> Record_Edit(BaseParam<RecordInfo, long> arg)
        {
            arg = Record_Edit_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Updateable<RecordInfo>()
                .SetColumns(x => new RecordInfo() { TagId = arg.Param.TagId, DataValue = arg.Param.DataValue })
                .Where(x => x.Id == arg.Param.Id)
                .ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<RecordInfo, long> Record_Delete(BaseParam<RecordInfo, long> arg)
        {
            arg = Record_Del_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Deleteable<RecordInfo>().Where(x => x.Id == arg.Id).ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<RecordInfo, long> Record_DeleteAll(BaseParam<RecordInfo, long> arg)
        {
            arg.Data = this.Context.Deleteable<RecordInfo>().ExecuteCommand();
            arg.Param = null;

            return arg;
        }
    }
}
